package cz.czu.pef.DataModelling.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.richfaces.model.selection.Selection;
import org.richfaces.model.selection.SimpleSelection;
import org.springframework.stereotype.Component;

@Component
@ViewScoped
@ManagedBean
public abstract class AbstractExtendedTableBean<T>  implements Serializable
{
	private static final long serialVersionUID = -2509946708890778188L;

	private String sortMode="single";
	private String selectionMode="multi";
	private Object tableState;
	private Selection selection = new SimpleSelection();
	protected List<T> items = new ArrayList<T>();
	
	protected PagingExtendedTableDataModel<T> dataModel;
	private List<T> selectedItems = new ArrayList<T>();
	
	public String getSortMode() {
		return sortMode;
	}

	public void setSortMode(String sortMode) {
		this.sortMode = sortMode;
	}

	public String getSelectionMode() {
		return selectionMode;
	}

	public void setSelectionMode(String selectionMode) {
		this.selectionMode = selectionMode;
	}

	public AbstractExtendedTableBean() {
	}
	
	public void takeSelection(){
		selectedItems.clear();
		Iterator<Object> iterator = getSelection().getKeys();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			selectedItems.add(getItemsDataModel().getObjectByKey(key));
		}
	}
	
	public abstract PagingExtendedTableDataModel<T> getItemsDataModel();

	public void setItems(List<T> items) {
		this.items = items;
	}

	public Object getTableState() {
		return tableState;
	}

	public void setTableState(Object tableState) {
		this.tableState = tableState;
	}

	public Selection getSelection() {
		return selection;
	}

	public void setSelection(Selection selection) {
		this.selection = selection;
	}

	public List<T> getSelectedItems() {
		return selectedItems;
	}

	public void setSelectedItems(List<T> selectedItems) {
		this.selectedItems = selectedItems;
	}

}
